<?php
namespace bxd\lib\validator;

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package lib
 * @subpackage validator
 */
class ValidatorManager
{
	/**
	 * Lista delle regole da validare
	 *
	 * @var array
	 */
	private $_rules	= array();

	/**
	 * Lista degli errori trovati dalla validazione
	 *
	 * @var array
	 */
	private $_errors = array();

	/**
	 * Lista degli errori aggiuntivi
	 *
	 * @var array
	 */
	private $_errorsCustom = array();
	
	private $_multipleField = array();

	/**
	 * Aggiunta di una nuova regola di validazione
	 *
	 * @param string $field Nome del campo
	 *
	 * @return Field
	 */
	public function addField($field)
	{
		$fieldName = ((is_array($field)) ? implode(",", $field) : $field);
		
		$this->_rules[$fieldName] = new Field($fieldName);
		
		if(is_array($field))
			$this->_multipleField[$fieldName] = $field;

		return $this->_rules[$fieldName];
	}

	/**
	 * Metodo che verifica tutti i campi aggiunti e restituisce il risultato del controllo
	 *
	 * @param array $data Array associativo chiave valore
	 *
	 * @return boolean
	 */
	public function validate($data = null)
	{
		$this->_errors = array();

		if(($data !== null && is_array($data)) || $data === null)
		{
			foreach($this->_rules as $fieldName => $rule)
			{			
				$error = false;
				
				if(array_key_exists($fieldName, $this->_multipleField))
				{
					foreach($this->_multipleField[$fieldName] as $postName)
					{
						$error = $this->_validateField($data, $rule, $postName);
						
						if($error !== false)
						{
							$this->_errors[$fieldName] = $error;
							break;
						}
					}
				}
				else
				{
					$error = $this->_validateField($data, $rule, $fieldName);
					
					if($error !== false)
						$this->_errors[$fieldName] = $error;
				}
			}
		}

		$this->_errors = array_merge($this->_errors, $this->_errorsCustom);		
		$this->_errorsCustom = array();
		
		return ((count($this->_errors)) ? false : true);
	}
	
	private function _validateField($data, $rule, $postName)
	{
		if(isset($data[$postName]))
			$rule->value($data[$postName]);

		if(!$rule->isValid())
			return $rule->getErrors();
		
		return false;
	}
	
	/**
	 * Metodo che permette di aggiungere un errore non generato dai validatori istanziati
	 *
	 * @param string $field Nome del campo
	 * @param string|integer $errCode Codice di errore
	 * @param string $errMess Messaggio di errore
	 */
	public function addError($field, $errCode, $errMess)
	{
		if(!isset($this->_errorsCustom[$field])) 
			$this->_errorsCustom[$field] = array();
		
		$this->_errorsCustom[$field][] = array
		(
			"code" => $errCode,
			"message" => $errMess
		);
	}

	/**
	 * Restituisce la lista degli errori per ogni campo settato
	 *
	 * @return array
	 */
	public function getErrors()
	{
		return $this->_errors;
	}

	/**
	 * Restituisce la lista degli errori per ogni campo settato
	 *
	 * @return array
	 */
	public function getErrorsCode()
	{
		return $this->_errors;
	}
}
?>
